package com.wuhongyu.controller;

import com.wuhongyu.entity.Contact;
import com.wuhongyu.service.ContactService;
import com.wuhongyu.service.CustomerService;
import com.wuhongyu.service.impl.ContactServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 联系人
 */
@Controller
@RequestMapping("/contact")
public class ContactController{

    @Autowired
    private ContactService contactService;
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/list")
    public String list(Model model,Integer customerid){
        model.addAttribute("contactList",((ContactServiceImpl)contactService).findAll(customerid));
        model.addAttribute("customer",customerService.findOne(customerid));
        return "~cust/cust/linkman";
    }

    @RequestMapping("/queryBefore")
    public String queryBefore(Model model, Integer id,Integer customerid) {
        if(id!=null && customerid==null){
            model.addAttribute("contact",contactService.findOne(id));
            return "~cust/cust/linkman_edit";
        }
        model.addAttribute("customer",customerService.findOne(customerid));
        return "~cust/cust/linkman_add";
    }


    @RequestMapping("/save")
    public String save(Model model, HttpServletRequest request, Contact contact) {
        contactService.save(contact);
        return this.list(model,contact.getCustomer().getId());
    }


    @RequestMapping("/delete")
    public String delete(Model model, HttpServletRequest request, Integer id) {
        Contact contact = contactService.findOne(id);
        contactService.delete(id);
        return this.list(model,contact.getCustomer().getId());
    }
}